<?php
/**
 * 临时需求
 */
namespace Wap\Controller;

use Common\Model\FormModel;
use Common\Model\RedpackModel;

use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Wap\BaseController;

class TmpController extends BaseController {


    /**
     * 生日会报名
     */
    public function index(){
        $uid=session('uid')+0;
        if(IS_AJAX){
            $data=array();
            $data['addTime']    = time();
            $data['uid']        = $uid;
            $data['parentName'] = I('post.realname');
            $data['phone'] = I('post.phone');
            $data['address'] = I('post.address');
            if(!$data['phone']){
                $this->ajaxReturn(array('msg'=>'家长的联系电话是必填项唷！'));
            }


            $nickname=I('post.nickname');
            $sex=I('post.sex');
            $birthday=I('post.birthday');

            for($i=0;$i<=2;$i++) {
                if (array_key_exists($i, $birthday)) {
                    if(!$nickname[$i]){
                        $this->ajaxReturn(array('msg'=>'宝宝的昵称是必填项唷！'));
                    }
                    if(!$birthday[$i]){
                        $this->ajaxReturn(array('msg'=>'宝宝的生日是必填项唷！'));
                    }
                    $data['babySex' . ($i + 1)] = $sex[$i];
                    $data['babyName' . ($i + 1)] = $nickname[$i];
                    $data['babyBirthday' . ($i + 1)] = $birthday[$i];
                }
            }
            $M=M('tmp_babyinfo',null,DB_MAIN_CFG);
            $M->add($data,null,true);
            $this->ajaxReturn(array('msg'=>'信息提交成功'));

        }
        else{

            if(!UserModel::checkFollow($uid)){
                $this->smarty->display("tmp/focus.html");
                die;
            }

            $M=M('tmp_babyinfo',null,DB_MAIN_CFG);
            $babyinfo=$M->where("uid={$uid}")->find();
            if($babyinfo){
                $this->smarty->assign('babyInfo',$babyinfo);
                $this->smarty->assign('title','信息查看');
            }else{
                $this->smarty->assign('title','信息填写');
            }

            $this->smarty->display("tmp/babyInfo.html");

        }
    }


    public function pet(){
        $uid=session('uid')+0;
        if(IS_AJAX){
            $data=array();
            $data['addTime']    = time();
            $data['uid']        = $uid;
            $data['realname'] = I('post.realname');
            $data['phone'] = I('post.phone');

            if(!$data['phone']){
                $this->ajaxReturn(array('msg'=>'联系电话是必填项唷！'));
            }


            $data['people']=I('post.people');
            $data['babyNum']=I('post.babyNum');
            $data['job']=I('post.job');
            $data['area']=I('post.area');

            $data['pet']=I('post.pet');
            $data['say'] = I('post.say');
            $M=M('tmp_pet',null,DB_MAIN_CFG);
            $M->add($data,null,true);
            $this->ajaxReturn(array('msg'=>'信息提交成功'));

        }
        else{
            if(!UserModel::checkFollow($uid)){
                $this->smarty->display("tmp/focus.html");
                die;
            }

            $M=M('tmp_pet',null,DB_MAIN_CFG);
            $babyinfo=$M->where("uid={$uid}")->find();
            if($babyinfo){
                $this->smarty->assign('babyInfo',$babyinfo);
            }
            $this->smarty->assign('title','宠物领养');
            $this->smarty->display("tmp/pet.html");

        }
    }



    public function  form(){
        $fid=I('get.fid',0,'intval');
        if(!$fid){
            $this->error('指定的表单错误！');
        }


        $uid=session('uid')+0;
        $form=FormModel::getFormInstance()->where("fid=".$fid)->find();
        if($form['mustFollow'] && !UserModel::checkFollow($uid)){
            $this->smarty->display("tmp/focus.html");
            die;
        }

        if(IS_AJAX){
            $fid=I('post.fid',0,'intval');
            $fields=explode(';',$form['content']);
            $valarr=array();
            foreach($fields as $key=>$one){
                list($fname,$ftypeParam,$must,$append)=explode('+',$one);
                list($ftype,$fparam)=explode(':',$ftypeParam);

                $val=I('post.f'.$key,'');
                if($must && $val==''){
                    $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 是必填项，请完成填写"));
                }
                switch ($ftype){
                    case FormModel::FIELD_DATE_FUTURE:
                        if(strtotime($val)>=strtotime(date('Y-m-d'))){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是今天或之后的日期"));
                        }
                        break;
                    case FormModel::FIELD_DATE_PAST:
                        if(strtotime($val)<=strtotime(date('Y-m-d'))){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是今天或之前的日期"));
                        }
                        break;
                    case FormModel::FIELD_TIME:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_NUM:
                            if(is_numeric($val)){
                                $valarr[]=$val;
                            }else{
                                $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是数值"));
                            }
                        break;
                    case FormModel::FIELD_TEXT:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_LONGTEXT:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_SINGLE:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_MULTI:
                        $valarr[]=implode(',',I('post.f'.$key));
                        break;
                    case FormModel::FIELD_EMAIL:
                        if(filedPreMatch('email', $val)){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 不是合格的邮箱地址"));
                        }
                        break;
                    case FormModel::FIELD_PHONE:
                        if(filedPreMatch('phone', $val)){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 不是合格的手机号码"));
                        }
                        break;
                    case FormModel::FIELD_PHOTO:
                        break;
                    case FormModel::FIELD_AREA:
                        $valarr[]=$val;
                        break;
                }
            }
            $data=array(
                'fid'=>$fid,
                'uid'=>$uid,
                'data'=>implode(';',$valarr),
                'addTime'=>time()
            );
            FormModel::getDataInstance()->add($data,null,true);

            unset($fields);
            $this->ajaxReturn(array('status'=>0,'msg'=>'success'));

        }





        $data=FormModel::getDataInstance()->where("uid={$uid} and fid={$fid}")->find();
        $fields=explode(';',$data['data']);

        $this->smarty->assign('data',$fields);

        $this->smarty->assign('title',$form['title']);
        $this->smarty->assign('fid',$fid);

        $fields=explode(';',$form['content']);
        $paramArray=array();
        foreach($fields as $one){
            list($fname,$ftypeParam,$must,$append)=explode('+',$one);
            list($ftype,$fparam)=explode(':',$ftypeParam);
            $paramArray[]=array(
                'fname'=>$fname,
                'ftype'=>$ftype,
                'fparam'=>$fparam,
                'must'=>$must,
                'append'=>$append
            );
        }
        unset($fields);

        $this->smarty->assign('params',$paramArray);

        $this->smarty->assign('FIELD_TEXT',FormModel::FIELD_TEXT);
        $this->smarty->assign('FIELD_LONGTEXT',FormModel::FIELD_LONGTEXT);
        $this->smarty->assign('FIELD_NUM',FormModel::FIELD_NUM);
        $this->smarty->assign('FIELD_DATE_FUTURE',FormModel::FIELD_DATE_FUTURE);
        $this->smarty->assign('FIELD_DATE_PAST',FormModel::FIELD_DATE_PAST);
        $this->smarty->assign('FIELD_TIME',FormModel::FIELD_TIME);
        $this->smarty->assign('FIELD_PHOTO',FormModel::FIELD_PHOTO);
        $this->smarty->assign('FIELD_SINGLE',FormModel::FIELD_SINGLE);
        $this->smarty->assign('FIELD_MULTI',FormModel::FIELD_MULTI);
        $this->smarty->assign('FIELD_PHONE',FormModel::FIELD_PHONE);
        $this->smarty->assign('FIELD_EMAIL',FormModel::FIELD_EMAIL);
        $this->smarty->assign('FIELD_AREA',FormModel::FIELD_AREA);


        $this->smarty->display("tmp/form.html");
    }



    private $redpackId=42;

    /**
     * 红包领取页
     */
    public function redpack(){
        if(!$this->isWechat){
           // die("请在微信中打开本链接");
        }
        $uid = session('uid');

        $now=time();
        if(RedpackModel::getChanceInstance()->where("uid={$uid} and setId={$this->redpackId} and type=0")->find()){
            //已经发过默认卡券
        }else{
            RedpackModel::getChanceInstance()->add(
                array('uid'=>$uid,'setId'=>$this->redpackId,'addTime'=>$now,'type'=>0,'status'=>0)
            );
        }


        $chanceTimes=RedpackModel::getChanceCount($uid,$this->redpackId,RedpackModel::CHANCE_STATUS_UNUSED);
        $this->smarty->assign('chanceTimes', $chanceTimes);

        $waveId=RedpackModel::getWaveInstance()->where("setId={$this->redpackId} and $now >= beginTime and endTime")->max('waveId');
        if(!$waveId){
            $waveId=0;
        }

        $list=$this->redpack_list($waveId);

        $this->smarty->assign('userList', $list);
        //微信分享相关
        $this->smarty->assign('isWxShare', true);
        $this->smarty->assign('wxTitle', "丸子妈妈新年送红包");
        $this->smarty->assign('wxDesc', "最高抢66.6吉利包，先到先得，抢完截止");
        $this->smarty->assign('wxLink', 'http://wzma.176wp.cn/wap.php?m=Wap&c=Tmp&a=redpack');
        $this->smarty->assign('wxImage', 'http://wzma.176wp.cn/Public/Wap/redpack/shareIcon.jpg');




        $this->smarty->display('tmp/redpack.html');
    }


    public function redpack_list($waveId){
        $listStr= <<<END
天天猫,1,1
不要吃兔兔,1,1
眸子,1,1
想不开的牛驼,1,1.8
丫丫丫,2,1.8
执子之手,2,1.8
我的地盘我做主,2,1.8
swq,3,1
狐狸尾巴,3,1
豆沙木头,3,1.8
草莓味的吻,1,1.8
一半惊喜一半遗憾,1,1.8
热情消退,1,1.8
依赖成折磨,1,6.6
爱不单行,1,3.6
爱到最后全是伤,1,3.6
没人要的野猫,1,18
十里温柔,2,1
安暖盛夏夜未央,2,3.6
南葵思暖,2,1
华灯初上,2,3.6
愿岁月可回头,2,18
青空如璃,2,6.6
刚好遇见你,2,36
繁花入梦,3,1.8
待你如故,3,3.6
如初〃,3,6.6
栀夏,3,6.6
一万里的晴空,3,18
月光倾城,3,36
樱*^o^*🍒,1,66.6
洛洛,2,66.6
candy,3,66.6
为你点赞,3,18
罗纳尔蛋,1,18
END;
        $records=explode("\n",$listStr);
        $list=array();
        foreach ($records  as $k=>$v){
            list($nickname,$wave,$money)=explode(',',$v);
            if($wave<=$waveId){
                $list[]=UtilsModel::maskNickname($nickname)." 获得 {$money}元";
            }
        }
        shuffle($list);
        return $list;
    }




    

    /**
     * 打开红包
     */
    public function redpackOpen(){
        $uid=session('uid');
        $nickname=session('nickname');
        $this->ajaxReturn(RedpackModel::useChance($uid,$nickname,$this->redpackId));
    }

    /**
     * 分享回调处理
     */
    public function redpackShare(){//限获得
        $uid=session('uid');
        if(!$uid){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请先登陆'));
        }
        $this->ajaxReturn(RedpackModel::addChance($uid,$this->redpackId));


    }

}